<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>药品验收</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>
<body>
	<script type="text/javascript">
		$.extend($.fn.datagrid.methods, {
			editCell : function(jq, param) {
				return jq.each(function() {
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields', true)
							.concat($(this).datagrid('getColumnFields'));
					for (var i = 0; i < fields.length; i++) {
						var col = $(this)
								.datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field) {
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for (var i = 0; i < fields.length; i++) {
						var col = $(this)
								.datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});

		var editIndex = undefined;
		function endEditing() {
			if (editIndex == undefined) {
				return true
			}
			if ($('#lfq-tb-drugYanshou').datagrid('validateRow', editIndex)) {
				$('#lfq-tb-drugYanshou').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickCell(index, field) {
			if (endEditing()) {
				$('#lfq-tb-drugYanshou').datagrid('selectRow', index).datagrid(
						'editCell', {
							index : index,
							field : field
						});
				editIndex = index;
			}
		}
	</script>

	<script type="text/javascript">
		$(function() {
			//刷新
			$("#lfq-drugYans-reload").linkbutton({
				onClick : function() {
					$("#lfq-tb-drugYanshou").datagrid({
						url : 'selectDrugYanshou',
						method : 'post'
					});
					$('#lfq-drugYanshou-search').searchbox("setValue","");
				}
			});
			
			//条件查询
			$('#lfq-drugYanshou-search').searchbox({
				searcher : function(value, name) {
					if (value == null || value.length == 0) {

					} else {
						if(name=="采购计划ID"){
							if(!isNaN(value)){
								$("#lfq-tb-drugYanshou").datagrid({
									url : 'selectDrugYanshouByLikeCgjhNo',
									method : 'post',
									queryParams : {
										cgjhNo : value
									}
								});
							}else{
								$.messager.alert("提示", "采购计划是由数字组成，请输入正确的编号！");
							}
							
						}else if(name=="药品名称"){
							$("#lfq-tb-drugYanshou").datagrid({
								url : 'selectDrugYanshouByLikeCpName',
								method : 'post',
								queryParams : {
									cpName : value
								}
							});
						}
					}
				}
			});
			
			$("#lfq-tb-drugYanshou").datagrid({onAfterEdit:function(index, row, changes){
				var kk=row.cgTongguoSun;
				if(parseInt(kk)>parseInt(row.cgNumber)){
					$('#lfq-tb-drugYanshou').datagrid('updateRow',{
						index: index,
						row: {
							cgTongguoSun: row.cgNumber
						}
					});
				}
			}});
			
			//提交数据
			$("#lfq-btn-DrugYansOk").linkbutton({
				onClick : function() {
					var rows=$("#lfq-tb-drugYanshou").datagrid("getRows");
					var arrs=[];
					var kk=0;
					for(var i=0;i<rows.length;i++){
						var hegeNumber=rows[i].cgTongguoSun;
						if(hegeNumber>0){
							arrs.push(rows[i].cgNo);
							arrs.push(rows[i].cgTongguoSun);
							kk++;
						}
					}
					
					if(kk>0){
						//创建表单提交数据
						$('<form>').form('submit', {
							url : 'drugYanShouAndTuiHuo',
							onSubmit : function(param) {
								param.arr = arrs;
							},
							success : function(data) {
								var json = eval("(" + data + ")");
								$.messager.alert("提示", json.reason);
								//重新加载表格
								$("#lfq-tb-drugYanshou").datagrid({
									url : 'selectDrugYanshou',
									method : 'post'
								});
								$('#lfq-drugYanshou-search').searchbox("setValue","");
							}
						});
						
					}else if(kk==0 && rows.length>0){
						$.messager.alert("提示", "请把要验收的药品给出合格数量！");
					}else if(kk==0 && rows.length==0){
						$.messager.alert("提示", "暂时还没有要验收的药品！");
					}
				}
			});
			
		});
	</script>

	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'center'" style="padding: 10px;">
			<table id="lfq-tb-drugYanshou" class="easyui-datagrid"
				data-options="
				    url:'selectDrugYanshou',
					fitColumns:true,
					fit:true,
					nowrap:false,
					pagination: true,
					pageSize: 5,
					pageList: [5, 10, 15, 20],
					rownumbers: true,
					onClickCell: onClickCell,
					toolbar: '#lfq-tool-yanshou',
					border:false
					">
				<thead>
					<tr>
					    <th data-options="field:'cgjhNo',width:'14%'">采购计划编号</th>
						<th data-options="field:'cgNo',width:'10%'">采购编号</th>
						<th data-options="field:'cpName',width:'17%'">产品名称</th>
						<th data-options="field:'ygsName',width:'17%'">供应商</th>
						<th data-options="field:'cgNumber',width:'8%'">数量</th>
						<th data-options="field:'cgUnit',width:'8%'">单位</th>
						<th data-options="field:'cgPrice',width:'8%'">价格</th>
						<th data-options="field:'cgTotalprice',width:'10%'">总价格</th>
						<th
							data-options="field:'cgTongguoSun',width:'8%',editor:{type:'numberbox',options:{precision:0,min:1}}"">合格数量</th>
					</tr>
				</thead>
			</table>

			<div id="lfq-tool-yanshou">
				<a id="lfq-drugYans-reload" class="easyui-linkbutton"
					data-options="iconCls:'icon-reload',plain:true">刷新</a> <input
					id="lfq-drugYanshou-search" class="easyui-searchbox"
					data-options="menu: '#lf1-ys', width: 260, prompt: '请输入信息'" />
			</div>

			<div id="lf1-ys" style="width: 120px">
				<div data-options="name:'采购计划ID'">采购计划ID</div>
				<div data-options="name:'药品名称'">药品名称</div>
			</div>
		</div>
		<div data-options="region:'south',border:false"
			style="text-align: right; padding: 5px 0 0;">
			<a id="lfq-btn-DrugYansOk" class="easyui-linkbutton"
				data-options="iconCls:'icon-ok', plain:true">确定验收</a> <a
				class="easyui-linkbutton"
				data-options="iconCls:'icon-cancel', plain:true"
				href="javascript:$('#lfq-purchasePlan-win-yanshou').window('close');"
				onclick="">取消</a>
		</div>
	</div>
</body>
</html>